package com.ww.springboot.boot.jvm;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.OperatingSystemMXBean;

public class SystemInfo {
    public static void main(String[] args) {
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();

        System.out.println("Heap memory usage: " + heapMemoryUsage);
        System.out.println("Non-Heap memory usage: " + nonHeapMemoryUsage);


        OperatingSystemMXBean osBean = java.lang.management.ManagementFactory.getOperatingSystemMXBean();
        if (osBean instanceof com.sun.management.OperatingSystemMXBean) {
            com.sun.management.OperatingSystemMXBean sunOsBean = (com.sun.management.OperatingSystemMXBean) osBean;
            long cpuLoad = (long) (sunOsBean.getSystemCpuLoad() * 100); // 转换为百分比
            System.out.println("CPU Load: " + cpuLoad + "%");
        } else {
            System.out.println("OperatingSystemMXBean does not support system CPU load");
        }
    }
}